package web.admin.shop.product.checkboxtree;


import application.WicketApplication;
import domain.shop.Category;
import org.apache.wicket.injection.web.InjectorHolder;
import org.apache.wicket.spring.injection.annot.SpringBean;
import service.facade.admin.ShopAdminService;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import java.io.Serializable;
import java.util.*;
import java.util.logging.Logger;

public class CategoryTreeModel extends DefaultTreeModel implements Serializable {

    private Locale locale;
    @SpringBean
    private transient ShopAdminService shopAdminService;
    private static Logger logger = Logger.getLogger(CategoryTreeModel.class.getName());

    public CategoryTreeModel() {
        super(new RootTreeNode());
        InjectorHolder.getInjector().inject(this);
    }

    public void setLocale(Locale locale) {
        this.locale = locale;
    }

    public Locale getLocale() {
        return locale;
    }

    @Override
    public Object getChild(Object parent, int index) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) parent;
        Object data = node.getUserObject();
        List<DefaultMutableTreeNode> children = new ArrayList<DefaultMutableTreeNode>();
        if (data instanceof CategoryModel) {
            children = getCategoryChildren((CategoryModel) data);
        } else {
            logger.info("WHAT DO WE HAVE HERE: " + data);
        }

        return children.get(index);
    }

    @Override
    public int getChildCount(Object parent) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) parent;
        Object data = node.getUserObject();
        List<DefaultMutableTreeNode> children = new ArrayList<DefaultMutableTreeNode>();
        if (data instanceof CategoryModel) {
            children = getCategoryChildren((CategoryModel) data);
        } else {
            logger.info("WHAT DO WE HAVE HERE: " + data);
        }
        return children.size();
    }

    @Override
    public boolean isLeaf(Object node) {
        return false;
    }

    @Override
    public void valueForPathChanged(TreePath path, Object newValue) {
    }

    @Override
    public int getIndexOfChild(Object parent, Object child) {
        DefaultMutableTreeNode parentTNode = (DefaultMutableTreeNode) parent;
        Object parentData = parentTNode.getUserObject();

        DefaultMutableTreeNode childTNode = (DefaultMutableTreeNode) child;
        Object childData = childTNode.getUserObject();

        List<DefaultMutableTreeNode> children = new ArrayList<DefaultMutableTreeNode>();
        if (parentData instanceof Category) {
            children = getCategoryChildren((CategoryModel) parentData);
        }

        boolean found = false;
        int idx = 0;
        for (Iterator<DefaultMutableTreeNode> i = children.iterator(); i.hasNext() && !found; idx++) {
            DefaultMutableTreeNode tnode = i.next();
            found = tnode.getUserObject().equals(childData);
        }

        return idx;
    }

    private List<DefaultMutableTreeNode> getCategoryChildren(CategoryModel parent) {
        List<DefaultMutableTreeNode> children = new ArrayList<DefaultMutableTreeNode>();
        List<Category> categories = new ArrayList<Category>();
        try {
            if (parent.getId() == null) {

                categories = getShopAdminService().findCategoryRoots();
            } else {
                categories = getShopAdminService().findCategoryChildsByParentId(parent.getId());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        Collections.sort(categories, new CategoryComparator());
        for (Category category : categories) {
            children.add(new CategoryTreeNode(category));
        }

        return children;
    }

    //kdyz se loadne v jinym tabu stranka, pak se klikne v editaci produktu na ulozit, tak bude shopAdminService null
    public ShopAdminService getShopAdminService() {
        if (shopAdminService == null) {
            shopAdminService = (ShopAdminService) WicketApplication.getInstance().getBean("shopAdminService");
        }
        return shopAdminService;
    }
}
